package com.juyu.miliao.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.constant.EnergyLevelType;
import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.domain.dto.FindCommentReplyInfoDTO;
import com.juyu.miliao.api.domain.vo.FindCommentReplyVO;
import com.juyu.miliao.api.domain.vo.ResultFindCommentReplyVO;
import com.juyu.miliao.api.service.*;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * 回复controller
 *
 * @author xuanqicheng
 * @create 2019-21-15:31
 */
@RestController
@RequestMapping("find/dyanmic/commentreply")
@CrossOrigin
public class FindCommentReplyInfoController {

    @Autowired
    private FindCommentReplyInfoService findCommentReplyInfoService;

    @Autowired
    private AppKeyService appKeyService;

    @Autowired
    private CheckFindDynamicCommentAndReplyService checkFindDynamicCommentAndReplyService;

    @Autowired
    private EnergyLevelService energyLevelService;

    /**
     * 新增回复评论
     *
     * @param token
     * @param commentReplyInfoDTO
     * @param bindingResult
     */
    @PostMapping("/")
    public FindCommentReplyVO addCommentReply(Authentication token, @Validated FindCommentReplyInfoDTO commentReplyInfoDTO,
                                              BindingResult bindingResult) throws IOException {

        if (bindingResult.hasErrors()) {
            throw new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();

        commentReplyInfoDTO.setReplyFromUserId(subject.getId());

        Appkey appkey = appKeyService.getAppKeyByKey("findDynamicCommentAndReplyNumber");

        checkFindDynamicCommentAndReplyService.checkNumber(subject.getId(),Integer.parseInt(appkey.getValue()));

        FindCommentReplyVO findCommentReplyVO = findCommentReplyInfoService.addCommentReply(commentReplyInfoDTO);
        Appkey energyKey = appKeyService.getAppKeyByKey("commentAndReplyObtainEnergy");

        energyLevelService.addUserEnergyLevel(subject.getId(), EnergyLevelType.ENERGY_TYPE_COMMENT,
                Integer.parseInt(energyKey.getValue()));

        return findCommentReplyVO;


    }

    @GetMapping("/{commentId}/{page}-{pageSize}")
    public ResultFindCommentReplyVO getDynamicInfoList(Authentication token, @PathVariable("commentId") long commentId,
                                                       @PathVariable("page") Integer page,
                                                       @PathVariable("pageSize") Integer pageSize) throws IOException {
        long userId = 0;
        if (pageSize <= 0) {
            pageSize = 20;
        }
        if (page <= 0) {
            page = 1;
        }

        if (token != null) {
            Subject subject = (Subject) token.getPrincipal();
            userId = subject.getId();
        }
        return findCommentReplyInfoService.selectPage(userId, commentId, page, pageSize);
    }

}
